﻿using System;
using System.Collections;
using System.Globalization;
using System.Web.UI;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using eStore.Business;
using Resources;

namespace eStore.UI
{
    public partial class ProductListLoaderControl : UserControl
    {
        #region Private Properties

        private int pageId = -1;
        private int moduleId = -1;
        private int pageNumber = 1;
        private int totalPages = 1;
        private int pageSize = 6;
        private Store store;
        private string siteRoot = string.Empty;
        protected string teaserFileBaseUrl = string.Empty;
        private CultureInfo currencyCulture = CultureInfo.CurrentCulture;
        eStoreConfiguration config = new eStoreConfiguration();
        private int categoryID = -1;
        readonly eStoreDB repository = new eStoreDB();

        #endregion

        #region Public Properties
        
        public int CategoryID
        {
            get { return categoryID; }
            set { categoryID = value; }
        }

        public int PageId
        {
            get { return pageId; }
            set { pageId = value; }
        }

        public int ModuleId
        {
            get { return moduleId; }
            set { moduleId = value; }
        }

        public Store Store
        {
            get { return store; }
            set { store = value; }
        }

        public string SiteRoot
        {
            get { return siteRoot; }
            set { siteRoot = value; }
        }

        public CultureInfo CurrencyCulture
        {
            get { return currencyCulture; }
            set { currencyCulture = value; }
        }

        public bool EnableRatings { get; set; }

        public bool EnableRatingComments { get; set; }

        public Hashtable Settings { get; set; }

        public eStoreConfiguration Config
        {
            get { return config; }
            set { config = value; }
        }

        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible) { return; }

            LoadSettings();

            if ((!Page.IsPostBack) && (ParamsAreValid()))
            {
                BindProducts();
            }

        }

        public string FormatPriceLabel()
        {
            return config.PriceLabelSetting != string.Empty ? config.PriceLabelSetting : eStoreResources.ProductPriceLabel;
        }

        private void BindProducts()
        {
            int totalRecords;
            rptProducts.DataSource = config.ShowSpecialProductsSetting
                                         ? (categoryID > 0
                                                ? repository.GeteStoreSpecialProductByCatIDPaging(store.Guid, categoryID,
                                                                                                  pageNumber,
                                                                                                  pageSize,
                                                                                                  out totalRecords)
                                                : repository.GeteStoreSpecialProductPaging(store.Guid, pageNumber,
                                                                                           pageSize,
                                                                                           out totalRecords))
                                         : (categoryID > 0
                                                ? repository.GeteStoreProductByCatIDPaging(store.Guid, categoryID,
                                                                                           pageNumber,
                                                                                           pageSize, out totalRecords)
                                                : repository.GeteStoreProductPaging(store.Guid, pageNumber, pageSize,
                                                                                    out totalRecords));
            rptProducts.DataBind();
            if (totalRecords.Equals(0))
            {
                pnlNoProduct.Visible = true;
                ltrInfo.Text = eStoreResources.NoProduct;
            }
            totalPages = ((totalRecords - 1) / pageSize) + 1;
            string pageUrl = SiteUtils.GetNavigationSiteRoot() + "/eStore/ProductList.aspx"
                             + "?pageid=" + pageId.ToInvariantString()
                             + "&amp;mid=" + moduleId.ToInvariantString()
                             + "&amp;pagenumber={0}";
            pgr.PageURLFormat = pageUrl;
            pgr.ShowFirstLast = true;
            pgr.CurrentIndex = pageNumber;
            pgr.PageSize = pageSize;
            pgr.PageCount = totalPages;
            pgr.Visible = (totalPages > 1);
        }

        private void LoadSettings()
        {
            SetJQueryScript();

            pageNumber = WebUtils.ParseInt32FromQueryString("pagenumber", pageNumber);

            if (Page is mojoBasePage)
            {
                mojoBasePage basePage = Page as mojoBasePage;
                basePage.ScriptConfig.IncludeYahooMediaPlayer = true;
            }

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings == null) { return; }

            teaserFileBaseUrl = WebUtils.GetSiteRoot() + "/Data/Sites/" + siteSettings.SiteId
                + "/estoreproductpreviewfiles/";

            

        }

        private bool ParamsAreValid()
        {
            if (store == null) { return false; }
            if (pageId == -1) { return false; }
            if (moduleId == -1) { return false; }


            return true;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += Page_Load;
        }

        protected string FormatDescription(string description)
        {
            if (SecurityHelper.RemoveMarkup(description).Length > config.DescriptionLengthSetting)
            {
                description = UIHelper.CreateExcerpt(description, config.DescriptionLengthSetting);
            }
            return description;
        }

        private void SetJQueryScript()
        {
            if (config.AutoScrollSetting)
            {
                ltrScript.Text = @"<script src='/ClientScript/jqmojo/jquery.tools.min.js' type='text/javascript'></script>";
                ltrAutoScrollCssSetup.Text = string.Empty;
            }
            if (!config.AutoScrollSetting) return;
            if (config.AutoScrollVerticalSetting)
            {
                ltrAutoScrollCssSetup.Text += @"<style type='text/css'>"
                                              + @".scrollable" + ModuleId + @"{"
                                              + @"position:relative; overflow:hidden; float:left;"
                                              + @"width:" + config.AutoScrollWidthWrapperSetting + @";"
                                              + @"height:" + config.AutoScrollHeightWrapperSetting + @";}"
                                              + @".scrollable" + ModuleId + @" .items{"
                                              + @"height:20000em; position:absolute;}"
                                              + @"</style>";
            }
            else
            {
                ltrAutoScrollCssSetup.Text += @"<style type='text/css'>"
                                              + @".scrollable" + ModuleId + @"{"
                                              + @"position:relative; overflow:hidden; float:left;"
                                              + @"width:" + config.AutoScrollWidthWrapperSetting + @";"
                                              + @"height:" + config.AutoScrollHeightWrapperSetting + @";}"
                                              + @".scrollable" + ModuleId + @" .items{"
                                              + @"width:20000em; position:absolute;}"
                                              + @".scrollable" + ModuleId + @" .items div{"
                                              + @"float: left;}"
                                              + @"</style>";
            }
            pnlScrollable.CssClass = "scrollable" + ModuleId;
            ltrScript.Text += @"<script type='text/javascript'>$(document).ready(function(){"
                              + @"$('.scrollable" + ModuleId + @"').scrollable({"
                              + @"circular: " + config.AutoScrollCircularSetting.ToString().ToLower()
                              + @", speed: " + config.AutoScrollSpeedSetting
                              + @", easing: '" + config.AutoScrollEasingSetting + @"'"
                              + @", steps: " + config.AutoScrollItemsSetting
                              + @", vertical: " + config.AutoScrollVerticalSetting.ToString().ToLower()
                              + @"}).autoscroll(" + config.AutoScrollTimeSetting + @");});</script>";
        }

    }
}